﻿namespace Arms.Framework.Objects.Geometry
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    [Serializable]
    public class Polygon : IEnumerable<Coordinate>
    {
        private List<Coordinate> _coordinates;

        public Polygon(params Coordinate[] coordinates) : this(coordinates.ToList()) { }
        public Polygon(IEnumerable<Coordinate> coordinates)
        {
            _coordinates = new List<Coordinate>();
            _coordinates.AddRange(coordinates);
        }

        public Coordinate this[int index]
        {
            get { return _coordinates[index]; }
        }

        public virtual void Add(Coordinate coordinate)
        {
            this.Add(coordinate);
        }

        public virtual void Add(params Coordinate[] coordinates)
        {
            this.Add(coordinates);
        }

        public virtual void Add(IEnumerable<Coordinate> coordinates)
        {
            _coordinates.AddRange(coordinates);
        }

        public IEnumerator<Coordinate> GetEnumerator()
        {
            return _coordinates.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        public override bool Equals(object obj)
        {
            if (obj == null || !(obj is Polygon))
            {
                return false;
            }

            Polygon polygon = (Polygon)obj;

            if (polygon.Count() != _coordinates.Count)
            {
                return false;
            }

            for (int i = 0; i < polygon.Count(); i++)
            {
                if (polygon[i] != _coordinates[i])
                {
                    return false;
                }
            }

            return true;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override string ToString()
        {
            return string.Join(",", _coordinates.Select(x => string.Format("{{0}}", x.ToString()).ToArray()));
        }
    }
}
